class_name ParticleWaterBullet
extends MagicBulletBase


@export var speed: float = 200
var dir: Vector2 = Vector2.RIGHT

@export var life_time: float = 0.5
var life_timer: float = 0

var is_destroy: bool = false
@onready var animation_player: AnimationPlayer = $AnimationPlayer

@onready var audio_water_bullet_shoot: AudioStreamPlayer2D = $AudioWaterBulletShoot



func _ready() -> void:
	audio_water_bullet_shoot.play()
	animation_player.play("show")


func _physics_process(delta: float) -> void:
	process_move(delta)
	process_destroy(delta)
	process_hit()
	

func process_destroy(delta: float):
	if is_destroy: return
	
	life_timer += delta
	if life_timer >= life_time:
		is_destroy = true
		animation_player.play("hide")
		await animation_player.animation_finished
		queue_free()
	

func process_move(delta: float):
	#if is_destroy: return
	
	position += dir * speed * delta
	
			
func _on_hit_area_body_entered(body: Node2D) -> void:
	var level = get_tree().current_scene as Level
	level.emit_particle("particle_water_hit_effect", global_position)
	level.camera.start_shake(hit_cam_shake_strength, hit_cam_shake_duration)
	
	dir = Vector2.ZERO
	is_destroy = true
	animation_player.play("hide")
	await animation_player.animation_finished
	queue_free()
	

func on_hit(source: Node2D):
	var level = get_tree().current_scene as Level
	level.emit_particle("particle_water_hit_effect", global_position)
